﻿using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Media;
using Shrestha.Data.EventModel;
using System;
using System.Linq;
using System.Windows.Shapes;
using System.Collections.Generic;
using Microsoft.Maps.MapControl.WPF;
using System.Windows.Input;
using Shrestha.Charts.Commands;
using System.Windows.Controls;
using System.Data;
using Shrestha.Charts.Graph;
using Shrestha.Charts.Info;

namespace Shrestha.Charts
{
    public class ChartWindowViewModel: IViewModel
    {
        public bool ShowLocationLines { get; set; }
        public bool ShowMetaLayer { get; set; }
        public bool ShowStoryLines { get; set; }

        public bool IsMetaAvailable { get; private set; }
        public bool IsCharacterAvailable { get; private set; }

        private readonly ChartWindow _cw;
        private DrawingManager _dm;

        public ICommand RedrawCommand { get; set; }
        public ICommand MetadataCommand { get; set; }
        public ICommand FilterCommand { get; set; }
        public ICommand UndoCommand { get; set; }
        public ICommand StorylineCommand { get; set; }
        public ICommand ResetCommand { get; set; }
        public ICommand LegendCommand { get; set; }
        public ICommand ScreenshotCommand { get; set; }

        private Runner _r;
        private DataView _dv;
        private DataView _dmv;

        public string Condition { get; set; }

        private TriGraph _tg;

        public ChartWindowViewModel(ChartWindow chartWindow, Runner r)
        {
            this._cw = chartWindow;
            this._r = r;
            this.IsMetaAvailable = _r.ContainsMetaTable;
            this.IsCharacterAvailable = _r.ContainsCharacters;
            Condition = "";
            init();
            _tg = new TriGraph(chartWindow, r) { StoryBrush = Brushes.Tomato};
        }

        private void init()
        {
            this.RedrawCommand = new RedrawCommand(this);
            this.MetadataCommand = new MetadataCommand(this);
            this.FilterCommand = new FilterCommand(this);
            this.UndoCommand = new UndoCommand(this);
            this.StorylineCommand = new StorylineCommand(this);
            this.ResetCommand = new ResetCommand(this);
            this.LegendCommand = new LegendCommand(this);
            this.ScreenshotCommand = new ScreenshotCommand(this);

            _dm = new DrawingManager(_cw.tgc, _r);
        }

        public void Draw()
        {
            if (_dv == null) Filter();
            _tg.DrawGraph(_dv, _dmv, _dm, ShowLocationLines, ShowMetaLayer);
        }


        public void Filter()
        {
            _dv = _r.GetCondition(_dv, Condition);
            if (IsMetaAvailable) _dmv = _r.GetMetaAfterUpdate();
            Draw();
        }

        public void Undo()
        {
            _dv = _r.Undo();
            if (IsMetaAvailable) _dmv = _r.GetMetaAfterUpdate();
            Draw();
        }

        public void SetCondition(string c)
        {
            Condition = c;
        }

        public bool CheckMeta()
        {
            return IsMetaAvailable;
        }

        public Runner GetRunner()
        {
            return _r;
        }


        public void ShowInfo()
        {
            if (_r.legend == null) return;

            Grid g = new Grid();
            g.Margin = new Thickness(10);

            //g.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
            //g.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
            //for (int i = 0; i < _r.legend.LegendList.Count; i++)
            //{
            //    g.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
            //}

            

            g.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
            for (int i = 0; i < _r.legend.LegendList.Count*2; i++)
            {
                g.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });    
            }


            int j = 0;
            foreach (LegendEntry le in _r.legend.LegendList)
            {
                Rectangle r = new Rectangle()
                {
                    Width = 8,
                    Height = 8,
                    StrokeThickness = 1,
                    Stroke = Brushes.Black,
                    Fill = ToMediaColor(le.color),
                    Margin = new Thickness(5)
                };
                Grid.SetRow(r, 0);
                Grid.SetColumn(r, j);
                g.Children.Add(r);

                TextBlock tb = new TextBlock()
                {
                    Text = le.misc,
                    FontSize = 12,
                    Margin = new Thickness(0,0,10,0)
                };
                Grid.SetRow(tb, 0);
                Grid.SetColumn(tb, j+1);
                g.Children.Add(tb);
                j+=2;
            }

            Window w = new Window()
            {
                Title = "Legend",
                Content = g,
                SizeToContent = SizeToContent.WidthAndHeight,
                WindowStyle = WindowStyle.ToolWindow,
                Owner = _cw
            };
            w.Show();
        }

        private Brush ToMediaColor(System.Drawing.Color color)
        {
            SolidColorBrush sbc = new SolidColorBrush(System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B));
            //BrushConverter bc = new BrushConverter();
            //Brush b = (Brush)bc.ConvertFrom(color.GetHashCode());
            return sbc;
        }


        public IWindow GetWindow()
        {
            return this._cw;
        }
    }
}
